package phone.qyg.com.qyg.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.app.application.QXApplication;
import com.app.base.tool.BaseActivity;
import com.app.config.AppHttpKey;
import com.app.config.ApplicationConfig;
import com.app.utils.PromptManager;
import com.app.utils.TransformController;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import phone.qyg.com.qyg.R;
import phone.qyg.com.qyg.model.CourseInterTrans;
import phone.qyg.com.qyg.model.CourseNote;
import phone.qyg.com.qyg.utils.AppHttpRequest;

/**
 * 作者：lblbh on 2017/12/8 18:43
 * 邮箱：lanbuhan@163.com
 * 功能：添加笔记
 */
@ContentView(R.layout.release_note)
public class CourseReleaseNoteActivity extends BaseActivity implements TextWatcher {

    @ViewInject(R.id.back)
    TextView back;

    @ViewInject(R.id.submit)
    TextView submit;

    @ViewInject(R.id.content)
    EditText content;

    String contentStr;

    int id;

    int type;

    @Override
    public void initSucessView() {
        Object obj = getTransModels();
        if(obj instanceof  CourseInterTrans){
            CourseInterTrans trans = (CourseInterTrans) obj;
            id = trans.id;
        }else if(obj instanceof CourseNote){
            CourseNote courseNote = (CourseNote) obj;
            String content = courseNote.content;
            contentStr = (isRequestStr(content)?content:courseNote.title);
            id = courseNote.id;
            type = 1;
        }
    }

    @Event(value = {R.id.back,R.id.submit})
    private void viewOnClick(View v){
        switch (v.getId()){
            case R.id.back:
                this.finish();
                break;
            case R.id.submit:
                String contentStr = getEditToString(content);

                if(!isRequestStr(contentStr)){
                    PromptManager.showToast(R.string.please_note_hint);
                    return;
                }
                submit.setEnabled(false);

                //进行网络请求
                setReleaseNote();
                break;
        }
    }

    private void setReleaseNote(){
        String contentStr = getEditToString(content);
        mapKeys.put((type != 1)?AppHttpKey.ID:AppHttpKey.N_ID,String.valueOf(id));
        mapKeys.put(AppHttpKey.CONTENT,contentStr);

        AppHttpRequest.appQuitState(ApplicationConfig.ADD_COURSE_NOTE,
                mapKeys, new AppHttpRequest.OnAppHttpListener() {
                    @Override
                    public void onAppHttpState(boolean isState, String result) {
                        if(isState){
                            PromptManager.showToast(R.string.release_note_sucess);
                            CourseReleaseNoteActivity.this.finish();

                            Intent intent = new Intent(ApplicationConfig.APP_QX_COURSE_NOTE);
                            intent.putExtra(TransformController.ChangeKEY.INTENT_KEY, result);
                            LocalBroadcastManager.getInstance(QXApplication.getContext()).sendBroadcast(intent);
                        }
                    }
                });
    }

    @Override
    public void initViewHandler() {
        content.addTextChangedListener(this);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        submit.setEnabled(false);
        submit.setBackgroundResource(R.drawable.ash_btn_frame);
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {


        if(isEmptyOrHintMinCount(content,R.string.please_note_hint,1)){
            submit.setEnabled(true);
            submit.setBackgroundResource(R.drawable.blue_btn_frame);
        }else{
            submit.setEnabled(false);
            submit.setBackgroundResource(0);
            submit.setBackgroundResource(R.drawable.ash_btn_frame);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {}

    @Override
    public void initData(Bundle savedInstanceState) {
        if(type == 1){
            content.setText(contentStr);
        }
    }
}
